import { Injectable } from '@nestjs/common';
import { fa_user } from '../entities/fa_user';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { hashPassword } from '../common/utils/hash';

@Injectable()
export class UserService {
  constructor(@InjectRepository(fa_user) private readonly repository: Repository<fa_user>) {}

  async findOneById(id: number) {
    return await this.repository.findOne(id);
  }

  async findOne(username: string) {
    return await this.repository.findOne({ username });
  }

  async changePassword(username: string, password: string): Promise<number> {
    const newPassword = await hashPassword(password);
    const result = await this.repository.update({ username }, { password: newPassword });
    return result.raw.changedRows;
  }
}
